package com.xiangge.domain;

import java.util.ArrayList;
import java.util.Scanner;

public class Crud {

    // 定义一个动态数组对象
    public static final ArrayList<Student> list = new ArrayList<>();

    public static int getIndex(ArrayList<Student> list, String sid){
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if(sid.equals(stu.getSid())){
                index = i;
            }
        }
        return index;
    }

    // 展示页面
    public void index(Scanner scanner) {
        // 管理界面重复出现，将其进行无限循环
        outer:
        while (true){
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");

            // 键盘输入（1-5）的整数进行选择
            int choice = scanner.nextInt();
            // 创建选择
            switch (choice){
                case 1: // 添加学生 addStudent
                    addStudent(list);
                    break;
                case 2: // 删除学生 deleteStudent
                    deleteStudent(list);
                    break;
                case 3: // 修改学生 updateStudent
                    updateStudent(list);
                    break;
                case 4: // 查看学生 queryStudent
                    queryStudent(list);
                    break;
                case 5: // 结束使用
                    System.out.println("感谢你的使用");
                    break outer;
                default:
                    System.err.println("你的输入有误");
                    break;
            }
        }
    }

    // 添加学生页面
    public void addStudent(ArrayList<Student> list){
        Scanner scanner = new Scanner(System.in);

        String sid;

        while(true){
            System.out.println("请输入学号:");
            sid = scanner.nextLine();
            int index = getIndex(list, sid);
            if(index == -1){
                break;
            }else{
                System.out.println("学号已被占用");
            }
        }
        System.out.println("请输入姓名:");
        String name = scanner.next();
        System.out.println("请输入年龄:");
        int age = scanner.nextInt();
        System.out.println("请输入生日:");
        String birthday = scanner.next();

        Student student = new Student(sid, name, age, birthday);

        list.add(student);

        System.out.println("添加学生成功");
    }

    // 删除学生页面
    public void deleteStudent(ArrayList<Student> list){
        // 键盘录入
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的学号：");
        String del = scanner.nextLine();

        int index = getIndex(list, del);

        if (index == -1){
            System.out.println("该信息不存在，请重新输入");
        }else {
            list.remove(index);
            System.out.println("删除成功");
        }
    }

    // 修改学生页面
    public void updateStudent(ArrayList<Student> list){
        // 键盘录入
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的学号：");
        String update = scanner.nextLine();

        int index = getIndex(list, update);

        if (index == -1){
            System.out.println("该信息不存在, 请重新输入");
        }else {
            System.out.println("请输入新的姓名：");
            String name = scanner.nextLine();
            System.out.println("请输入新的年龄：");
            int age = scanner.nextInt();
            System.out.println("请输入新的生日：");
            String birthday = scanner.nextLine();

            Student Student = new Student(update,name,age,birthday);

            list.set(index, Student);

            System.out.println("修改成功");
        }
    }

    // 查看学生页面
    public void queryStudent(ArrayList<Student> list){
        // 键盘录入
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要查询的学号（或输入'all'查看所有学生）:");
        String input = scanner.nextLine();

        if ("all".equalsIgnoreCase(input)) {
            // 显示所有学生信息
            for (Student student : list) {
                System.out.println(student);
            }
        } else {
            // 根据学号查询学生信息
            int index = getIndex(list, input);
            if (index != -1) {
                Student student = list.get(index);
                System.out.println("查询到的学生信息: " + student);
            } else {
                System.out.println("未找到学号为 " + input + " 的学生");
            }
        }
    }
}
